package com;


import com.fz.entity.Book;
import com.fz.entity.Student;
import com.fz.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by webrx on 2017/6/13.
 */
public class Dome {
    @Test
    public void show(){
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession session = sqlSessionFactory.openSession();
        System.out.println(session);

        StudentMapper sdao = session.getMapper(StudentMapper.class);

        List<Book> list = session.selectList("com.fz.mapper.BookMapper.queryAll");
        System.out.println(list.size());
        for(Book book : list){
            System.out.println(book.getName());
        }
      /* List<Book> list = session.selectList("com.fz.mapper.BookMapper.queryAll");
        for(Book book : list){
            System.out.println(book.getName());
        }*/

        //添加数据
        Student st = new Student();
        st.setName("赵六");
        st.setAddress("天津");
        int r = sdao.save(st);


        //提交会话事务
        session.commit();

        //数据查询
        List<Student> list01 = sdao.query();
       /* System.out.println(list.size());*/
        for(Student stu : list01){
            System.out.println(stu.getName());
        }
    }
}
